home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Text / WASTE / WASTE 1.2a2 / WEArrays.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-12  |  1.3 KB  |  59 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    WEArrays.c
  3.  *
  4.  *    WASTE PROJECT
  5.  *  Utilities for handling handle-based dynamic arrays
  6.  *
  7.  *  Copyright (c) 1993-1995 Marco Piovanelli
  8.  *    All Rights Reserved
  9.  *
  10.  *  C port by Dan Crevier
  11.  *
  12.  */
  13.  
  14.  
  15. #include "WASTEIntf.h"
  16.  
  17. #ifndef __ERRORS__
  18. #include <Errors.h>
  19. #endif
  20.  
  21. #ifndef __MEMORY__
  22. #include <Memory.h>
  23. #endif
  24.  
  25. pascal OSErr _WEInsertBlock(Handle h, const void *blockPtr, long blockSize, long offset)
  26. {
  27.     long oldSize;
  28.     OSErr err;
  29.  
  30. // get handle size
  31.     oldSize = InlineGetHandleSize(h);
  32.     WEASSERT((offset >= 0) && (offset <= oldSize), "\p_WEInsertBlock: bad offset");
  33.  
  34. // make room for the block to be inserted
  35.     SetHandleSize(h, oldSize + blockSize);
  36.     if ((err = MemError()) != noErr)
  37.         return err;
  38.     BlockMoveData( *h + offset, *h + offset + blockSize, oldSize - offset );
  39.  
  40. // insert block
  41.     BlockMoveData( blockPtr, *h + offset, blockSize);
  42.     
  43.     return noErr;
  44. }
  45.  
  46. pascal void _WERemoveBlock(Handle h, long blockSize, long offset)
  47. {
  48.     long newSize;
  49.  
  50. // get handle size minus a "slot"
  51.     newSize = InlineGetHandleSize(h) - blockSize;
  52.     WEASSERT((offset >= 0) && (offset <= newSize), "\p_WERemoveBlock: bad offset");
  53.  
  54. // compact the handle (this should never fail)
  55.     BlockMoveData( *h + offset + blockSize, *h + offset, newSize - offset );
  56.     SetHandleSize(h, newSize);
  57.     WEASSERT(MemError() == noErr, "\p_WERemoveSlot: failed to shorten handle");
  58. }
  59.